import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    component: () => import('@/views/HomeView.vue'),
    children: [
      {
        path: 'album/:id',
        component: () => import('@/views/AlbumDetail.vue'),
        props: true
      },
      {
        path: 'faces/:faceId',
        component: () => import('@/views/FaceDetail.vue'),
        beforeEnter: (to) => {
          // 验证人脸ID有效性
          if(!isValidFaceId(to.params.faceId)) {
            return '/404'
          }
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from) => {
  // 页面访问统计
  logPageView(to.path)
  // 检查用户权限
  if(to.meta.requiresAuth && !isAuthenticated()) {
    return '/login'
  }
})

const lazyLoadView = (view) => {
  return () => import(`@/views/${view}.vue`)
}

const adminRoutes = {
  path: '/admin',
  component: lazyLoadView('AdminPanel'),
  meta: { requiresAuth: true }
}

export default router